package com.scaffold.authority.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.scaffold.authority.entity.ResourceAuthorityRelation;

import wangjg.commons.page.Page;

@Repository
public class ResourceAuthorityRelationDAO extends
		BasicBizDAO<ResourceAuthorityRelation, Long> {

	public List<ResourceAuthorityRelation> findByResourceId(Long resourceId) {
		return this.findListByProperty("resourceId", resourceId);
	}

	public List<ResourceAuthorityRelation> findByAuthorityId(Long authorityId) {
		return this.findListByProperty("authorityId", authorityId);
	}

	public List<ResourceAuthorityRelation> pageQuery(Page page, Long resourceId, Long authorityId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		if (resourceId != null) {
			criteria.add(Restrictions.eq("resourceId", resourceId));
		}
		if (authorityId != null) {
			criteria.add(Restrictions.eq("authorityId", authorityId));
		}
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

}
